package org.game.module.summonbo;

import com.sojoys.artifact.exception.AbstractLogicModelException;
import com.sojoys.artifact.factory.session.ISession;
import com.sojoys.artifact.tools.ToolError;
import org.cfg.SummonCfg;
import org.cfg.SummonComposeCfg;
import org.cfg.cache.DiscreteDataCfgCache;
import org.cfg.cache.SummonCfgCache;
import org.cfg.cache.SummonComposeCfgCache;
import org.constant.DiscreteDataID;
import org.constant.DiscreteDataKey;
import org.error.GameErrorCode;
import org.game.constant.SessionConstent;
import org.game.db.bean.ComposeSummon;
import org.game.db.bean.Player;
import org.game.db.cache.ComposeSummonCache;
import org.game.manager.ItemManager;
import org.game.manager.SummonManager;
import org.game.msgBuilder.SummonMsgBuilder;
import org.game.protobuf.c2s.C2SSummonMsg;
import org.game.util.ProtoUnit;

import java.util.List;
import java.util.Map;

/**
 * 召唤
 * Created by fxf on 2017-12-23.
 */
public class SummonBOImpl implements SummonBO {
    @Override
    public byte[] summon(ISession session, C2SSummonMsg.Summon msg) throws AbstractLogicModelException {
        Player player = session.getAttr(SessionConstent.PLAYER);
        checkPlayerLevel(player.getLevel());
        int summonId = msg.getCid();
        int costType = msg.getCost();
        //1.校验
        SummonCfg summonCfg = SummonCfgCache.me().getById(summonId);
        //校验summonId
        ToolError.isAndTrue(GameErrorCode.CLIENT_PARAM_IS_ERR, "召唤id错误,id=" + summonId, summonCfg == null);

        List cost = summonCfg.getCost();
        ToolError.isAndTrue(GameErrorCode.CLIENT_PARAM_IS_ERR, "召唤消耗类型错误,costType=" + costType+",实际长度="+cost.size(), costType >= cost.size());
        Map realCost = (Map) cost.get(costType);
        //校验消耗
        ToolError.isAndTrue(GameErrorCode.CLIENT_PARAM_IS_ERR, "召唤消耗类型错误,costType=" + costType, realCost == null);

        return SummonManager.summon(player, summonCfg, realCost);
    }

    @Override
    public byte[] composeSummon(ISession session, C2SSummonMsg.ComposeSummon msg) throws AbstractLogicModelException {
        Player player = session.getAttr(SessionConstent.PLAYER);
        checkPlayerLevel(player.getLevel());

        int cid = msg.getCid();
        SummonComposeCfg cfg = SummonComposeCfgCache.me().getById(cid);
        ComposeSummon composeSummon = ComposeSummonCache.me().getByZPointTypePlayerId(cfg.getZPointType(),player.getId());
        ToolError.isAndTrue(GameErrorCode.COMPOSE_ING,"该质点正在合成",composeSummon != null);
        ToolError.isAndTrue(GameErrorCode.COMPOSE_COST_IS_NOT_ENOUGH,"消耗资源不足",!ItemManager.checkGoodsByCid(player,cfg.getCost()));

        return SummonManager.composeSummon(player,cfg);
    }

    @Override
    public byte[] composeFinish(ISession session, C2SSummonMsg.ComposeFinish msg) throws AbstractLogicModelException {
        Player player = session.getAttr(SessionConstent.PLAYER);
        int zPointType = msg.getZPointType();
        ComposeSummon composeSummon = ComposeSummonCache.me().getByZPointTypePlayerId(zPointType,player.getId());
        ToolError.isAndTrue(GameErrorCode.CLIENT_PARAM_IS_ERR,"没有找到合成对象",composeSummon == null);
        ToolError.isAndTrue(GameErrorCode.COMPOSE_IS_NOT_FINISH,"合成未完成",composeSummon.getFinishTime().getTime()> System.currentTimeMillis());

        return SummonManager.composeFinish(player,composeSummon);
    }

    @Override
    public byte[] getComposeInfo(ISession session) throws AbstractLogicModelException {
        Player player = session.getAttr(SessionConstent.PLAYER);
        return ProtoUnit.toByte(SummonMsgBuilder.buildGetComposeInfo(player));
    }

    //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ private method ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
    private void checkPlayerLevel(int playerLevel){
        int levelLimit = DiscreteDataCfgCache.me().getIntData(DiscreteDataID.SUMMON,DiscreteDataKey.SUMMON_PLAYER_LEVEL);
        ToolError.isAndTrue(GameErrorCode.ALREADY_EQUIP,"召唤：玩家等级不足",playerLevel < levelLimit);
    }
}
